﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WCF;
using System.Web.Security;

namespace geekmeet
{
    /// <summary>
    /// The page thats only visible to premium users. Functionality: click contest, how many 
    /// mouse clicks can you do on 15 seconds? 
    /// </summary>
    public partial class PremiumPage : System.Web.UI.Page
    {
        GeekMeetService wcf = new GeekMeetService();
        Guid loggedInUserGUID = (Guid)Membership.GetUser().ProviderUserKey;

        protected void Page_Load(object sender, EventArgs e)
        {
            //Checks if premium, if not --> default.aspx
            bool IsPremium = Roles.IsUserInRole("premiumuser");
            if (IsPremium)
            {
                ShowHighscoreList();
                LblIfHighest.Visible = false;
                //If the game is over
                if (Request.QueryString["action"] == ("gameover"))
                {
                    gameoverlabel.Visible = true;
                    gameoverlabel.Text = "<h3>Game over!<h3>";
                    LinkBtnGameover.Visible = true;
                    LinkBtnGameover.Text = "Klicka här för att testa igen!";
                    int result = int.Parse(Request.QueryString["result"]);

                    //Checks if the new result is the highest
                    if (result > GetHighestResult())
                    {
                        LblIfHighest.Visible = true;
                        LblIfHighest.Text = "Grattis, du har slagit rekordet!";
                        wcf.AddNewHighscore(loggedInUserGUID, result);
                    }
                    //Checks if it is the users highest score
                    else if (wcf.AddNewHighscore(loggedInUserGUID, result))
                    {
                        LblIfHighest.Text = "Nytt personbästa!";
                    }
                    else
                    {
                        wcf.AddNewHighscore(loggedInUserGUID, result);
                    }
                }
                else
                {
                    gameoverlabel.Visible = false;
                    LinkBtnGameover.Visible = false;
                }
                ShowHighscoreList();
            }
            else {
                Response.Redirect("~/Default.aspx");
            }
        }
        /// <summary>
        /// Shows the highscore list
        /// </summary>
        private void ShowHighscoreList()
        {
            List<ClickHighscoreComposite> highscore = wcf.getHighscore();
            HighscoreRepeater.DataSource = highscore;
            HighscoreRepeater.DataBind();
        }

        /// <summary>
        /// Gets highest score
        /// </summary>
        /// <returns>int of score points</returns>
        private int GetHighestResult()
        {
            if (wcf.getHighscore().Count == 0)
                return 0;
            else
                return wcf.getHighscore().First().Highscore;
        }
    }
}